package com.antdesignbackend.backend.form.farmersDaily.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.antdesignbackend.backend.activiti.constant.Constants;
import com.antdesignbackend.backend.config.WebConfig;
import com.antdesignbackend.backend.form.animalDaily.entity.AnimalDaily;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import com.antdesignbackend.backend.common.web.BaseController;
import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.aop.AuthToken;
import com.antdesignbackend.backend.form.farmersDaily.entity.FarmersDaily;
import com.antdesignbackend.backend.form.farmersDaily.entity.entityVo.FarmersDailyVo;
import com.antdesignbackend.backend.form.farmersDaily.service.FarmersDailyService;

/**
 * 养殖场(户)日常巡查记录
 *
 * @author 林宇杰
 * @email farmersDaily
 * @date 2020-08-10 17:44:59
 */

@Controller
@RequestMapping("/farmersDaily")
@Slf4j
public class FarmersDailyController extends BaseController{

	private static final String FUN_NAME = "养殖场(户)日常巡查记录";//功能名称

	@Autowired
	private FarmersDailyService farmersDailyService;

	@GetMapping("/index")
	@AuthToken
	public String index() {
		return "form/farmersDaily/index";
	}


	@PostMapping("/tableSearch")
	@ResponseBody
	ReturnVO tableSearch(@RequestBody FarmersDailyVo farmersDailyVo) throws SecurityException, IllegalArgumentException{
		ReturnVO result=new ReturnVO();
		List<FarmersDaily> farmersDailies=new ArrayList<>();
		QueryWrapper<FarmersDaily> wrapper =new QueryWrapper<>();
		if (farmersDailyVo.getTimeInterval() != null && !farmersDailyVo.getTimeInterval().equals("")){
			String[] str=farmersDailyVo.getTimeInterval().split(" - ");
			String star=str[0];
			String end=str[1];
			wrapper.between("inspectionDate",star,end);
		}
		wrapper.orderByDesc("inspectionDate").orderByDesc("CreateTime");
		//先统计满足条件的数据总数，因为sql 不包括排序
		int total=farmersDailyService.count(wrapper);
		result.setTotal(total);
		System.out.println("*******************************满足条件的数据有："+total);
		//再查询
		if(total>=0)
		{
			farmersDailies=farmersDailyService.page(new Page<FarmersDaily>(farmersDailyVo.getPageIndex(),farmersDailyVo.getPageSize()),wrapper).getRecords();
		}
		result.setData(farmersDailies);
		return result;
	}

	@GetMapping("/getById/{id}")
	@ResponseBody
	public	FarmersDaily getById(@PathVariable(name = "id") String id) {
		FarmersDaily obj = farmersDailyService.getById(id);
		return obj;
	}

	@PostMapping("/save")
	@ResponseBody
	public ReturnVO save(@RequestBody FarmersDaily farmersDaily){
		ReturnVO result = new ReturnVO();

//		farmersDaily.setTypes(Constants.PENDINGTRIAL);//审批中
		farmersDaily.setCreateBy(getLoginUserId());
		farmersDaily.setCreateTime(new Date());
//		farmersDaily.setEid(getLoginEnterpriseId());
		try {
			farmersDailyService.save(farmersDaily);
		}catch (Exception e){
			result.setMessage("保存失败！");
			result.setCode("500");
		}
		return result;
	}

	@PostMapping("update")
	@ResponseBody
	public ReturnVO updateById(@RequestBody FarmersDaily farmersDaily){
		ReturnVO result = new ReturnVO();
//		farmersDaily.setTypes(Constants.PENDINGTRIAL);//审批中
		farmersDaily.setCreateBy(getLoginUserId());
		farmersDaily.setCreateTime(new Date());
//		farmersDaily.setEid(getLoginEnterpriseId());
		try {
			farmersDailyService.updateById(farmersDaily);
		}catch (Exception e){
			result.setMessage("保存失败！");
			result.setCode("500");
		}
		return result;
	}

	/**
     * 根据id删除
     */
	@DeleteMapping("/deleteById/{id}")
	@ResponseBody
	public ReturnVO deleteById(@PathVariable Serializable id) {
		ReturnVO result = new ReturnVO();
		if (farmersDailyService.removeById(Integer.parseInt(id.toString())) == true) {
			result.setMessage("删除成功");
		} else {
			result.setMessage("删除失败");
			result.setCode("0");
		}
		return result;
	}

}
